package seabattle.ship;

import java.util.LinkedList;

public class Ship {
    private LinkedList<Coordinates> decks;
    private Coordinates disposal;
    private Direction direction;
    private int length;


    public Ship(Coordinates disposal, int length, Direction direction) {
        this.disposal = disposal;
        this.length = length;
        this.direction = direction;
        decks = new LinkedList<Coordinates>();
        buildDecks();
    }

    public Coordinates getDisposal() {
        return disposal;
    }

    public int getLength() {
        return length;
    }

    public Direction getDirection() {
        return direction;
    }

    public int amountAliveDecks() {
        return decks.size();
    }

    public boolean isItYoursDeck(Coordinates coordinates) {
        for (Coordinates deck : decks) {
            if (deck.equals(coordinates)) {
                return true;
            }
        }
        return false;
    }

    public boolean destroySpecifiedDeck(Coordinates coordinates) {
        if (!isItYoursDeck(coordinates) && decks.size() < 1) return false;
        for (int index = 0; index < length; index++) {
            if (decks.get(index).equals(coordinates)) {
                decks.remove(index);
                break;
            }
        }
        return true;
    }

    private void buildDecks() {
        int x = disposal.getX();
        int y = disposal.getY();
        for (int index = 0; index < length; index++) {
            if (direction.isVertical()) {
                addDeck(new Coordinates(x, y + index));
            } else {
                addDeck(new Coordinates(x + index, y));
            }
        }
    }

    private void addDeck(Coordinates deckDisposal) {
        decks.add(deckDisposal);
    }

}
